package com.shopping.servelet;

import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.shopping.entity.Goods;

@WebServlet("/ShoppingServelet")
public class ShoppingServelet extends HttpServlet{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("Text/html");
		HttpSession session=req.getSession(true);		
		String str="[";
		String str1="";
		if(session.getAttribute("list1")!=null && ((List<Goods>)session.getAttribute("list1")).size()!=0){
			List<Goods> list=(List<Goods>) session.getAttribute("list1");
			for (Goods goods : list) {
				str+="{\"id\":\""+goods.getId()+"\",\"name\":\""+goods.getName()+"\",\"price\":"+goods.getPrice()*goods.getNum()+",\"num\":"+goods.getNum()+"},";
				//str->[{"id":"1","name"："海澜","price":100,"num":1},{},
				//str+="{\"id\":"+"\""+goods.getId()+"\""+","+"\"name\":"+"\""+goods.getName()+"\""+","+"\"price\":"+"\""+goods.getPrice()+"\""+","+"\"num\":"+goods.getNum();
			}
			str1=str.substring(0, str.length()-1);
			str1+="]";
		}else{
			str1="";
		}
		
		Writer out=resp.getWriter();
		out.write(str1);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doGet(req, resp);
	}

}
